<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: lin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\Operation as OperationModel;
use think\facade\View;

/**
 * 系统操作管理
 * Class Operation
 * @package app\admin\controller
 */
class Operation extends AdminController
{
    /**
     * 操作列表
     * @author lin
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $operationModel = new OperationModel();
            $data = input('param.');
            if (isset($data['parent_id']) && $data['parent_id'] != "") {
                //此处不需要做任何操作
            } else {
                $data['parent_id'] = $operationModel::MENU_MANAGE;
            }
            return $operationModel->tableData($data);
        }
        return View::fetch();
    }


    /**
     * 操作删除
     * @author lin
     * @return array|mixed
     */
    public function del()
    {
        if (!input('?param.id')) {
            return error_code(10000);
        }
        $operationModel = new OperationModel();
        return $operationModel->toDel(input('param.id'));
    }


    /**
     * 操作的新增和编辑
     * @author lin
     */
    public function add()
    {
        $return = [
            'status' => false,
            'msg' => '失败',
            'data' => ''
        ];
        View::layout(false);
        $operationModel = new OperationModel();
        if ($this->request->isPost()) {
            return $operationModel->toAdd(input('param.'));
        } else {
            //如果是编辑，取数据
            if (input("?param.id")) {
                $info = $operationModel->where(['id'=>input('param.id')])->find();
                if (!$info) {
                    return error_code(10000);
                }
            } else {
                $info = [
                    'id' => '',
                    'parent_id' => '',
                    'name'=> '',
                    'code' => '',
                    'parent_menu_id' => '',
                    'type'=> '',
                    'perm_type'=> '',
                    'sort'=> '',
                ];
            }

            //取全树
            $list = $operationModel->where('type', '<>', 'a')->order('sort asc')->select()->toArray();
            $tree = $operationModel->createTree($list, $operationModel::MENU_START, 'parent_id');

            //取菜单树
            $menuList = $operationModel->where('perm_type', '<', 3)->order('sort asc')->select()->toArray();
            $menuTree = $operationModel->createTree($menuList, $operationModel::MENU_START, 'parent_menu_id');

            $return['status'] = true;
            $return['msg'] = '成功';
            $return['data'] = View::fetch('add', ['info' => $info, 'tree' => $tree, 'menuTree' => $menuTree]);
            return $return;
        }
    }
}
